package com.gc.controller.textbookServlet;


import com.gc.entity.TextBook;
import com.gc.repository.TextBookRepository;
import com.gc.repository.impl.TextBookRepositoryImpl;
import com.gc.util.result.R;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet(name = "textbookUpdateById", value = "/textbookUpdateById" )
public class textbookUpdateById extends HttpServlet {
    TextBookRepository textbookRepository = new TextBookRepositoryImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        TextBook textbook = new TextBook(Long.valueOf(request.getParameter("id")),
                request.getParameter("bookid"),
                request.getParameter("name"),
                request.getParameter("ISBN"),
                request.getParameter("publisher"),
                new BigDecimal(request.getParameter("price")));
        boolean res = textbookRepository.UpdateById(textbook);
        if(res) {
            if(res) {
                response.getWriter().write(gson.toJson(R.ok()));
            } else {
                response.getWriter().write(gson.toJson(R.error()));
            }
        }
    }
}
